/**
 * Created by walkman on 2017/7/27.
 */
/**
 * @angular 管理员
 * @description
 * @auth yang li
 */

interest = function ($scope, $ducTools, $compile, http, $dataTableSet) {
    'use strict';

    var interestTable = $('#interestTable').DataTable({
        sAjaxSource: 'interest/query',
        fnServerData: $ducTools.retrieveData,
        fnServerParams: function (aoData) {
            aoData.push(
                {"name": "ruleId", "value": $scope.ruleId},
                {"name": "interestDateStart", "value": $("#interestDateStart").val()},
                {"name": "interestDateEnd", "value": $("#interestDateEnd").val()}
            );
        },
        columns: [
            {"data": "interestId"},
            {"data": "ruleId"},
            {"data": "interestDate"},
            {"data": "interest"},
            {"data": null},
        ],
        columnDefs: [
            {
                targets: 1,
                render: function (c) {
                    if (c == "1") {
                        return "期消模式";
                    } else if (c == "2") {
                        return "月消模式";
                    }
                }
            },
            {
                targets: 4,
                render: function (c) {
                    return "<button class='btn btn-success btn-xs' ng-click=showDetail('" + c.interestId + "')> 查看/编辑</button> &nbsp;" +
                        "<button class='btn btn-success btn-xs' ng-click=deleteInterest('" + c.interestId + "')>删除</button> &nbsp;";
                }
            }
        ],
        createdRow: function (row, data, dataIndex) {
            $compile(row)($scope);
        }
    });

    $dataTableSet.dataTableRowShow('interestTable');

    $scope.interestRecordDraw = function () {
        interestTable.draw();
    };

    //详情
    $scope.showDetail = function (id) {

        $("#showDetail").modal('show');

        $("#site_tip").css("display", "none");

        $scope.interestId = "";
        $scope.ruleId = "";
        $scope.interestDate = "";
        $scope.interest = "";
        $("#interestDate").val("");

        //详情
        http.post({"tid": id}, "interest/get").then(function (data) {

            $scope.interestId = data.interestId;
            $scope.ruleId = data.ruleId;
            $scope.interestDate = data.interestDate;
            $scope.interest = data.interest;
            $("#interestDate").val(data.interestDate);
        });
    };

    //添加修改
    $scope.addInterest = function (id) {
        $scope.interestId = "";
        $(id).modal('show').find("form")[0].reset();

        $("#interestDate").val(new Date());
    };

    //删除
    $scope.deleteInterest = function (id) {
        swal({
            title: "确定要删除吗？",
            showCancelButton: true,
            closeOnConfirm: false,
            confirmButtonText: "确定",
            confirmButtonColor: "#046dae",
            cancelButtonText: "取消",
            animation: "slide-from-top"
        }, function () {
            http.post({"interestId": id}, 'interest/delete').then(function (res) {
                swal("删除成功！");
                interestTable.draw();
            });
        });
    };

    $("#interestForm").validate({
        rules: {
            ruleId: 'required',
            interestDate: 'required'
        },
        messages: {
            ruleId: {
                required: "请选择消费模式！"
            },
            interestDate: {
                required: "请选择日期！"
            }
        },
        focusInvalid: false,
        onkeyup: false,
        submitHandler: function () {
            var _url;
            var data;
            if ($scope.interestId == "") {
                _url = 'interest/add'
                data = {
                    "ruleId": $scope.ruleId,
                    "interestDate": $("#interestDate").val(),
                    "interest": $scope.interest
                };
            } else {
                _url = 'interest/update'
                data = {
                    "interestId": $scope.interestId,
                    "ruleId": $scope.ruleId,
                    "interestDate": $("#interestDate").val(),
                    "interest": $scope.interest
                };
            }

            http.post(data, _url).then(function (result) {
                $('#showDetail').modal('hide');
                swal("保存成功！");
                interestTable.draw();
            });
        },
        errorPlacement: function (error, element) {
            var interestFormErrorMsg = $("#interestFormErrorMsg");
            if (!interestFormErrorMsg.find('label').is(':visible')) {
                interestFormErrorMsg.html(error);
            }
            return false;
        }
    });
};
